﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Gizmox.WebGUI.Forms;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;

namespace PLACEHR.Web.Utilities
{
    public class Utility
    {
        public static string ConvertLengthToString(decimal length)
        {
            if (length > 0)
            {
                string days = (int)Math.Floor(length) / 8 == 0 ? "" : ((int)Math.Floor(length) / 8).ToString() + LanLayer.GetTrans(141);
                string hours = (int)Math.Floor(length) % 8 == 0 ? "" : ((int)Math.Floor(length) % 8).ToString() + LanLayer.GetTrans(142);
                string minutes = (length - (int)Math.Floor(length)) == 0 ? "" : ((int)Math.Ceiling((length - Math.Floor(length)) * 60)).ToString() + LanLayer.GetTrans(143);

                string strRet = string.Format("{0}{1}{2}", days, hours,minutes);
                return strRet;
            }
            return "";
        }

        public static byte[] ToBytes(Stream stream)
        {
            var bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        public static decimal ToDecimal(string input)
        {
            decimal output = 0;
            decimal.TryParse(input, out output);
            return output;
        }
        public static int ToInt(string input)
        {
            int output = 0;
            int.TryParse(input, out output);
            return output;
        }

        public static DateTime ToDateTime(string input)
        {
            return DateTime.Parse(input);
        }
        public static bool IsDateTime(string input)
        {
            try
            {
                DateTime.Parse(input);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool IsDecimal(string input)
        {
            try
            {
                var count = decimal.Parse(input);
                if (count <= 0)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool IsInt(string input)
        {
            try
            {
                var count = int.Parse(input);
                if (count <= 0)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static void SetRequestCookie(string cookieName, string cookieValue)
        {
            //set request cookie
            if (System.Web.HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                System.Web.HttpContext.Current.Request.Cookies[cookieName].Value = cookieValue;
            }
            else
            {
                System.Web.HttpContext.Current.Request.Cookies.Add(new System.Web.HttpCookie(cookieName, cookieValue));
            }

            ////set response cookie
            //if (System.Web.HttpContext.Current.Response.Cookies[cookieName] != null)
            //{
            //    System.Web.HttpContext.Current.Response.Cookies[cookieName].Value = cookieValue;
            //}
            //else
            //{
            //    var cookie = new System.Web.HttpCookie(cookieName, cookieValue);
            //    cookie.Expires = DateTime.MaxValue;
            //    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            //}
        }

        public static List<DictionaryEntry> GetOfficeList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.Offices.Where(p => !p.IsDeleted).ToList())
            {
                itemList.Add(new DictionaryEntry(item.OfficeCode,  LanLayer.GetTrans(item.ContentId.Value)));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetDeptList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.Departments.Where(p => !p.IsDeleted).ToList())
            {
                itemList.Add(new DictionaryEntry(item.DepartmentCode, LanLayer.GetTrans(item.ContentId.Value)));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetStatusCodeList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.StatusCode_lkp.ToList())
            {
                itemList.Add(new DictionaryEntry(item.StatusCode, LanLayer.GetTrans(item.ContentId.Value)));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetMaritalCodeList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.MaritalStatus_lkp.ToList())
            {
                itemList.Add(new DictionaryEntry(item.MaritalStatusCode, LanLayer.GetTrans(item.ContentId.Value)));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetEmployeeTypeList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.EmployeeType_lkp.ToList())
            {
                itemList.Add(new DictionaryEntry(item.EmployeeTypeCode, LanLayer.GetTrans(item.ContentID.Value)));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetBankList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.Bank_lkp.ToList())
            {
                itemList.Add(new DictionaryEntry(item.BankCode, LanLayer.GetTrans(item.ContentId.Value)));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetRoleList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.Roles.Where(o=>o.RoleId < 4).ToList())
            {
                itemList.Add(new DictionaryEntry(item.RoleId.ToString(), item.RoleName));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetRateCodeList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.RateCode_lkp.ToList())
            {
                itemList.Add(new DictionaryEntry(item.RateCode.ToString(), LanLayer.GetTrans(item.ContentId.Value)));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetVacationTypeList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.VacationType_lkp.ToList())
            {
                itemList.Add(new DictionaryEntry(item.VacationTypeCode, LanLayer.GetTrans(item.Content_id.Value)));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetHuKouList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.HuKou_lkp.ToList())
            {
                itemList.Add(new DictionaryEntry(item.HuKouCode, LanLayer.GetTrans(item.ContentID.Value)));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetContentCategoryList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> { new DictionaryEntry { Key = "", Value = "" } };
            foreach (var item in Global.Context.ContentCategories.ToList())
            {
                itemList.Add(new DictionaryEntry(item.CategoryCode, item.CategoryCode));
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetGenderList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> 
            { 
                new DictionaryEntry { Key = "", Value = "" } ,
                new DictionaryEntry { Key = "M", Value = "Male" } ,
                new DictionaryEntry { Key = "F", Value = "Female" } 
            };

            return itemList;
        }

        public static List<DictionaryEntry> GetYearList(int num)
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> 
            { 
                new DictionaryEntry { Key = "", Value = "Select..." }
            };

            int currYear = DateTime.Now.Year;
            for (int i = 0; i < num; i++)
            {
                itemList.Add(new DictionaryEntry { Key = (currYear - i).ToString(), Value = (currYear - i).ToString() });
            }

            return itemList;
        }

        public static List<DictionaryEntry> GetMonthList()
        {
            List<DictionaryEntry> itemList = new List<DictionaryEntry> 
            { 
                new DictionaryEntry { Key = "", Value = "Select..." }
            };

            for (int m = 1; m <= 12; m++)
            {
                itemList.Add(new DictionaryEntry { Key = m.ToString(), Value = m.ToString() });
            }

            return itemList;
        }

    }

    public class ComboBoxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }
        public override string ToString()
        {
            return this.Text;
        }
    }
    public class DataResourceHandle : Gizmox.WebGUI.Common.Resources.GatewayResourceHandle
    {
        public DataResourceHandle(byte[] imageData, string contentType)
            : base(new Gizmox.WebGUI.Common.Gateways.GatewayReference(new DataComponent(imageData, contentType), "SendData"))
        {
        }

        private class DataComponent : Gizmox.WebGUI.Forms.Component
        {
            private byte[] data;
            private string contentType;

            public DataComponent(byte[] data, string contentType)
            {
                this.data = data;
                this.contentType = contentType;
                this.RegisterSelf();
            }

            public DataComponent(System.IO.Stream stream, string contentType)
            {
                this.data = GetStreamAsByteArray(stream);
                this.contentType = contentType;
                this.RegisterSelf();
            }

            protected override Gizmox.WebGUI.Common.Interfaces.IGatewayHandler ProcessGatewayRequest(HttpContext objHttpContext, string strAction)
            {
                if (strAction == "SendData")
                {
                    // Set ContentType
                    if (contentType != null)
                        objHttpContext.Response.ContentType = contentType;
                    // Disable Caching
                    objHttpContext.Response.Expires = -1;
                    // Send Data to Browser         
                    objHttpContext.Response.BinaryWrite(data);

                    objHttpContext.Response.Flush();
                    objHttpContext.ApplicationInstance.CompleteRequest();

                    // Clean up
                    data = null;
                }
                return null;
            }

            private byte[] GetStreamAsByteArray(System.IO.Stream stream)
            {
                int streamLength = Convert.ToInt32(stream.Length);

                byte[] fileData = new Byte[streamLength];

                // Read the Stream into a Byte array
                stream.Position = 0;
                stream.Read(fileData, 0, streamLength);
                stream.Close();

                return fileData;
            }

            protected override void Dispose(bool disposing)
            {
                // Remove Myself from the Current VWG Application
                this.UnRegisterSelf();

                base.Dispose(disposing);
            }
        }
    }
    public class JpegResourceHandle : DataResourceHandle
    {
        public JpegResourceHandle(byte[] jpegData) : base(jpegData, "image/jpeg") { }
    }
}
